---
description: "Reference for Nitric's Dart library - Send a message to a connected websocket client"
---

# Dart - websocket.send()

<Note>
  This is reference documentation for the Nitric Dart SDK. To learn about
  Websockets in Nitric start with the [Websockets docs](/websockets).
</Note>

Send a message to a connected websocket client.

```dart
import 'package:nitric_sdk/nitric.dart';

final socket = Nitric.websocket("socket");

await socket.send(connectionId, message);
```

## Parameters

<Properties>
  <Property name="connectionId" required type="String">
    The ID of the connection which should receive the message.
  </Property>
  <Property name="message" required type="Map<String, dynamic>">
    The message that should be sent to the connection.
  </Property>
</Properties>

## Examples

### Broadcasting a message to all connections.

<Note>
  Do not send messages to a connection during it's `connect` callback, if you
  need to acknowledge connection, do so by using a [topic](../topic/topic)
</Note>

```dart
import 'package:nitric_sdk/nitric.dart';

final socket = Nitric.websocket("socket");
final connections = Nitric.kv("connections").allow([
  KeyValueStorePermission.get,
  KeyValueStorePermission.set,
  KeyValueStorePermission.delete,
]);

socket.onConnect((ctx) async {
  await connections.set(ctx.req.connectionId, {
    // add metadata
    "creation_time": DateTime.now(),
  });

  return ctx;
});

socket.onDisconnect((ctx) async {
  await connections.delete(ctx.req.connectionId);
});

// broadcast message to all clients (including the sender)
socket.onMessage((ctx) async {
  final conns = await connections.keys();

  await Future.wait(await conns.map((conn) async {
    // Send a message to a connection
    return socket.send(conn, ctx.req.message);
  }).toList());

  return ctx;
});
```
